package com.bnzj.cloud.business.core.aspect;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.bnzj.core.action.ActionType;
import com.bnzj.core.action.annotation.Action;
import com.bnzj.core.action.log.ActionLog;
import com.bnzj.core.action.log.ActionLogService;
import com.bnzj.core.rest.CurrentUserService;
import com.bnzj.core.rest.UserInfo;
import com.bnzj.core.util.JsonUtils;

/**
 * @author Barry
 * @createDate 2020/4/9
 * @description
 */
@Aspect
@Component
public class ActionLogAspect {

    @Resource
    private CurrentUserService currentUserService;
    
    @Resource
    private ActionLogService actionLogService;

    @Pointcut(value = "@annotation(com.bnzj.core.action.annotation.Action)")
    public void actionPointcut() {
    }

    @Before("actionPointcut()")
    public void beforeAction(JoinPoint point) {
        //获取注解属性值
        Action actionLog = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(Action.class);
        String descriprion = actionLog.descriprion();
        String moduleName = actionLog.moduleName();
        ActionType actionTypeConstants = actionLog.operateType();
        //行为名称
        String action = actionTypeConstants.getMessage();

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        //获取ip
        String ip = request.getRemoteAddr();
        //获取请求路径
        String url = request.getServletPath();
//        获取请求方式
        String method = request.getMethod();
        //获取参数
        Enumeration<String> enumeration = request.getParameterNames();
        Map<String, String> parameterMap = new HashMap<String, String>();
        while (enumeration.hasMoreElements()) {
            String parameter = enumeration.nextElement();
            parameterMap.put(parameter, request.getParameter(parameter));
        }
        String params = JsonUtils.objToJson(parameterMap);
        UserInfo userInfo = currentUserService.getUserInfo();
        saveLog(userInfo.getAccount(), userInfo.getName(), ip, url, method, params, moduleName, action, descriprion);
    }

    @Async
    public void saveLog(String account, String name, String ip, String url, String method, String parameter, String moduleName, String action, String desc) {
        ActionLog actionLog = actionLogService.createActionLog();
        actionLog.setIp(ip);
        actionLog.setUrl(url);
        actionLog.setMethod(method);
        actionLog.setParameter(parameter);
        actionLog.setModuleName(moduleName);
        actionLog.setActionName(desc);
        actionLog.setAction(action);
        actionLog.setAccount(account);
        actionLog.setUserName(name);
        actionLogService.add(actionLog);
    }
}
